<template>
  <div class="project-list">
    <div class="list-header">
      <el-button type="primary" size="small" @click="dialogFormVisible = true">新建</el-button>
    </div>
    <el-table :data="projectList" max-height="650" size="small" style="width: 100%">
      <el-table-column prop="project_name" label="项目名称" width="180">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleSelect(scope.row)">{{
            scope.row.project_name
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        v-for="col in tableColumns"
        :prop="col.prop"
        :label="col.label"
        :width="col.width"
        :key="col.prop"
      >
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="180">
        <template slot-scope="scope">
          {{ scope.row.create_time | format }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            circle
            @click="handleEdit(scope.$index, scope.row)"
          ></el-button>

          <el-popconfirm
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @onConfirm="handleDelete(scope.$index, scope.row)"
          >
            <el-button
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right; margin-top: 8px">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageParams.page"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="totalCount"
      >
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" :title="title">
      <div>
        <el-form ref="form" :model="form" label-width="80px" label-position="right" :rules="rules">
          <el-form-item label="项目名称" prop="project_name">
            <el-input v-model="form.project_name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="project_desc">
            <el-input v-model="form.project_desc" :rows="6" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="队列:" prop="pool">
            <el-select v-model="form.pool" placeholder="请选择" filterable>
              <el-option
                v-for="item in queueList"
                :key="item.id"
                :label="item.pool"
                :value="item.pool"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="成员:" prop="create_by">
            <el-select v-model="form.create_by" placeholder="请选择" filterable>
              <el-option
                v-for="item in userList"
                :key="item.id"
                :label="item.department_name + '-' + item.user_name"
                :value="item.oa_user_name"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addProject">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "ProjectList",
  data() {
    return {
      rules: {
        project_name: [{ required: true, message: "请输入项目名", trigger: "blur" }]
      },
      queueList: [],
      userList: [],
      projectList: [],
      totalCount: 0,
      dialogFormVisible: false,
      formLabelWidth: 80,
      tableColumns: [
        {
          prop: "project_desc",
          label: "描述"
        },
        {
          prop: "pool",
          label: "队列",
          width: "180"
        },
        {
          prop: "create_by",
          label: "管理员",
          width: "180"
        }
      ],
      tableData: [],
      form: {
        project_name: "",
        project_desc: "",
        id: "",
        pool: "",
        create_by: ""
      },
      pageParams: {
        page: 1
      }
    };
  },
  computed: {
    title() {
      return !this.form.id ? "新建项目" : "编辑项目";
    }
  },
  watch: {
    dialogFormVisible(val) {
      if (!val) {
        this.form.id = "";
        this.$refs.form.resetFields();
      }
    }
  },
  created() {
    this.initData();
    this.$http.queue.getQueueList().then(res => {
      this.queueList = res;
    });
    this.$http.member.getAlluser().then(res => {
      this.userList = res;
    });
  },
  methods: {
    ...mapMutations(["setCurrentProject"]),
    handleCurrentChange(val) {
      this.pageParams.page = val;
      this.initData();
    },
    addProject() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let { project_name, project_desc, id } = this.form;
          let result = null;
          if (id) {
            // 编辑
            result = this.$http.project.updateProject({
              ...this.form
            });
          } else {
            // 新增
            result = this.$http.project.addProject({
              project_name,
              project_desc
            });
          }
          let message = id ? "更新成功" : "新增成功";
          result.then(res => {
            this.$message({
              message,
              type: "success"
            });
            this.initData();
            this.dialogFormVisible = false;
          });
        } else {
          return false;
        }
      });
    },
    initData() {
      this.$http.project
        .pageProjectList({
          ...this.pageParams
        })
        .then(res => {
          this.totalCount = res.total;
          this.projectList = res.items;
        });
    },
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.$http.project.getProjectById({ id: row.id }).then(res => {
        Object.keys(this.form).forEach(key => {
          this.form[key] = res[key];
        });
      });
    },
    handleDelete(index, row) {
      let { id } = row;
      this.$http.project.deleteProject({ id }).then(res => {
        this.$message({
          message: "删除成功",
          type: "success"
        });
        this.initData();
      });
    },
    handleSelect(project) {
      this.setCurrentProject(project);
      this.$router.push({
        name: "data",
        query: {
          project_id: project.id,
          project_name: project.project_name
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.project-list {
  overflow: hidden;
  .list-header {
    float: right;
    margin-bottom: 8px;
  }
}

.el-table__header-wrapper .el-table__header th {
  background-color: #f8f8f9;
  color: #515a6e;
  font-size: 14px;
  font-weight: 500;
}
</style>
